package order

import (
	"OnlineRetailers/controllers/admin/PageNum"
	session2 "OnlineRetailers/controllers/admin/session"
	"OnlineRetailers/controllers/errorDeal"
	"OnlineRetailers/controllers/login"
	"OnlineRetailers/models"
	"OnlineRetailers/models/Admin/resMes"
	"OnlineRetailers/models/User"
	error2 "OnlineRetailers/models/errorDeal"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
	"strconv"
)

type GetOrder struct {
	beego.Controller
}

func (c *GetOrder) GetOrder() {
	defer func() {
		c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
		c.Ctx.Output.Status = 200
		c.ServeJSON()
	}()
	var addr = "/Order/GetOrder"
	var openid string
	res := resMes.GetOrder{}
	order := []*User.Order{}
	log := &error2.Log{}
	mes := &error2.Mes{}
	var err error
	o := orm.NewOrm()
	Page, err := c.GetInt("Page", 0)
	begin, err := c.GetInt("begin", -1)
	state, err := c.GetInt("state", -1)
	if err != nil {
		c.Data["json"] = errorDeal.ErrMess(log, "400", err, addr, err.Error())
		return
	}
	//fmt.Println(begin)
	if begin == -1 {
		err = session2.CheckSession(c.Ctx.Request.Header["Session"], 2)
		if err != nil {
			c.Data["json"] = errorDeal.ErrMess(log, "400", err, addr, err.Error())
			return
		}
	} else {
		err, openid = login.CheckOpenid(c.Ctx.Request.Header["Openid"])
		if err != nil {
			c.Data["json"] = errorDeal.ErrMess(log, "400", err, addr, err.Error())
			return
		}
	}

	//fmt.Println(begin)
	var sql string
	var num int64
	if begin == -1 {
		sql = "select * from `order`  order by `order_id` desc  limit ? offset ?"
		num, err = o.Raw(sql, models.AdminPage, models.AdminPage*Page).QueryRows(&order)
		count, err := PageNum.GetPageNum("order", o)
		if err != nil {
			c.Data["json"] = errorDeal.ErrMess(log, "400", err, addr, "GetPageNum"+err.Error())
			return
		}
		res.Count = count / models.AdminPage
	} else {
		if state == 0 {
			sql = "select * from `order`  where user_id=? order by `order_id` desc limit ? , ?"
			num, err = o.Raw(sql, openid, begin, begin+models.DateLengthByOrder).QueryRows(&order)
		} else {
			sql = "select * from `order`  where user_id=? and state=? order by `order_id` desc  limit ? , ? "
			num, err = o.Raw(sql, openid, strconv.Itoa(state), begin, begin+models.DateLengthByOrder).QueryRows(&order)
		}

	}

	if err != nil {
		c.Data["json"] = errorDeal.ErrMess(log, "400", err, addr, err.Error())
		return
	}
	if num == 0 {
		c.Data["json"] = errorDeal.ErrLightMess(mes, "201", "noMore")
		return
	}

	sql = "select * from `order_cart` left join `order_order_carts` on order_order_carts.order_cart_id=order_cart.order_cart_cart_id where order_order_carts.order_id=?"
	for i := 0; i < len(order); i++ {

		_, err := o.Raw(sql, order[i].OrderId).QueryRows(&order[i].OrderCart)
		//fmt.Println(n)
		if err != nil {
			c.Data["json"] = errorDeal.ErrMess(log, "400", err, addr, err.Error())
			return
		}
		for _, orderCart := range order[i].OrderCart {
			err = o.Read(orderCart.Product)
			if err != nil {
				c.Data["json"] = errorDeal.ErrMess(log, "400", err, addr, err.Error())
				return
			}
		}

	}

	res.Order = order
	res.Code = "200"
	c.Data["json"] = res

}
